package SanZiQi;
import java.util.Random;
import java.util.Scanner;

class play_chess {

    public static void play_chess1(char[][]array,int x,int y,char c) {
        Scanner p=new Scanner(System.in);
        System.out.println("请输入坐标");
        int a=p.nextInt();
        int b=p.nextInt();
        while(((a<1&&a>=x)||(b<1&&b>=y))||array[a-1][b-1]!=' ') {
            System.out.println("坐标被占");
            System.out.println("请重新输入");
            a=p.nextInt();
            b=p.nextInt();
        }
        array[a-1][b-1]=c;
    }
   public static int sleep(char c) {
       if(c=='*'){
           return 1;//玩家赢
       } else {
           return 0;//电脑赢
       }
   }
}
//实现游戏

public class game extends play_chess{
public static int count=0;
   public static char c='*';
    public static char d='#';
    public static  void initialize(char[][]array, int x, int y) {
        for (int i = 0; i <x ; i++) {
            for (int j = 0; j <y ; j++) {
                array[i][j]=' ';
            }
        }
    }//打印
    public static void print(char[][]array,int x,int y) {
        for (int i = 0; i <x ; i++) {
            for (int j = 0; j <y ; j++) {
                System.out.print(" ");
                System.out.print(array[i][j]);
                System.out.print(" ");
                if(j+1!=y) {
                    System.out.print("|");
                }
            }
            System.out.println();
        if(i+1!=x) {
            for (int j = 0; j <y ; j++) {
                System.out.print("---");
                if(j+1!=y) {
                    System.out.print("+");
                    }
                }
            }System.out.println();
        }
    }
    //玩家下棋（写到一个类）game player
    public static void game_Player(char[][]array,int x,int y) {
      play_chess1(array,x,y,c);
      count++;
    }
    //电脑下棋
    public static void game_Ai(char[][]array,int x,int y) {
        Random rd = new Random();
       while(true) {
           int p1= rd.nextInt(x);
           int p2= rd.nextInt(y);
           if(array[p1][p2]==' ') {
               x=p1;
               y=p2;
               break;
           }
       }
        array[x][y]=d;
       count++;
    }

    //写一个万能的比较的方法
    public static int judgeWu(char[][]array,int x,int y) {
        int i=0,j=0;
        int cop=0;
        for ( i = 0; i < x; i++) {
            cop=0;
            for ( j = 0; j < x - 1; j++) {
                if (array[i][j] != array[i][j + 1]) {
                    break;
                }

                cop++;
            }
            if(cop==4&&array[i][j-1]!=' ') {
                return sleep(array[i][j-1]);
            }
            cop=0;
            for ( j = 0; j < x - 1; j++) {
                if (array[j][i] != array[j + 1][i]) {
                    break;
                }
                cop++;
            }
            if(cop==4&&array[j-1][i]!=' ') {
                return sleep(array[j-1][i]);
            }
        }
         int cop1=0;
        for ( i = 0; i < x-1; i++) {
            for ( j = 0; j < y - 1; j++) {
                if (i == j) {
                    if (array[i][j] != array[i + 1][j + 1]) {
                        break;
                    }
                   cop1++;
                }
                if(cop1==4&&array[x/2][y/2]!=' ') {
                    return sleep(array[x/2][y/2]);
                }
            }
        }
        //单独写一个交叉判断的
        int k=x-1;
        int cop2=0;
            for (int l = 0; l <y-1 ; l++) {
                if(array[k][l]!=array[k-1][l+1]) {
                    break;
                }
                k--;
                cop2++;
        }
            if(array[x/2][y/2]!=' '&&cop2==4) {
                return sleep(array[x/2][y/2]);
            }

        if(count==x*y) {
            return 2;
        }
        return -1;//游戏继续
    }
    public static int panDan(char[][]array, int x, int y) {

        switch (judgeWu(array, x, y))
        {
            case 0:
                System.out.println("AI胜利");
                return 1;
            case 1:
                System.out.println("玩家胜利");
                return 1;
            case 2:
                System.out.println("平局");
                return 1;
            default:
                break;
        }
        return -1;
    }
}
//+---+---+---+
//| 0 |   |   |
//+---+---+---+
//|   |   |   |
//+---+---+---+
//|   |   |   |
//+---+---+---+

//   |   |
//---+---+---
//   |   |
//---+---+---
//   |   |
//public static int judgeSan(char[][]array,int x,int y) {
//    for (int i = 0; i < x; i++) {
//        if(array[i][0]==array[0][1]&&array[0][1]==array[0][2]&&array[i][0]!=' '){
//            return sleep(array[i][0]);
//        }
//    }
//    for (int i = 0; i <x ; i++) {
//        if(array[0][i]==array[1][i]&&array[1][i]==array[2][i]&&array[0][i]!=' '){
//            return sleep(array[0][i]);
//        }
//    }
//    if(array[1][1]==array[2][2]&&array[2][2]==array[0][0]&&array[1][1]!=' ')
//        return sleep(array[1][1]);
//    if(array[0][2]==array[1][1]&&array[1][1]==array[2][0]&&array[1][1]!=' ') {
//        return sleep(array[1][1]);
//    }
//    if(count==x*y) {
//        return 2;//游戏继续
//    }
//    return -1;
//}